/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controllers;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import java.util.Vector;

import Models.LoaiKH_Model;
import Beans.NhanVien;
import Models.NhatKyHT_Model;

/**
 *
 * @author Venus
 */
public class LoaiKH_Controller extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        try {
            if (session.getAttribute("langid") == null) {
                response.sendRedirect("index.jsp");
                return;
            }
            session.removeAttribute("DSMA");
            session.removeAttribute("DSNMA");
            session.removeAttribute("item");
            String action = request.getParameter("action");
            int langid = (Integer) session.getAttribute("langid");
            if (action.equals("Show")) {
                String item = request.getParameter("item");
                LoaiKH_Model lkhm = new LoaiKH_Model();
                Vector vDSLKH = new Vector();
                vDSLKH = lkhm.LayDSLKH(langid);
                request.setAttribute("DSLKH", vDSLKH);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/newcustomertype.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("CheckTenLKH")) {
                String customertype = request.getParameter("customertype");
                if (LoaiKH_Model.CheckTenLKH(customertype, langid)) {
                    out.print("exist");
                } else {
                    out.print("error");
                }
            }
            else if (action.equals("Add")) {
                String customertype = request.getParameter("customertype");
                String mark = request.getParameter("mark");
                String reduction = request.getParameter("reduction");
                int MaLKH = LoaiKH_Model.TaoMa();
                LoaiKH_Model.AddLoaiKH(MaLKH, customertype, mark, reduction);
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(2, nv.getMaNV(), "customertype");
                LoaiKH_Model lkhm = new LoaiKH_Model();
                Vector v = new Vector();
                v = lkhm.LayCustomerType(MaLKH);
                request.setAttribute("LangCT", v);
                RequestDispatcher rd = request.getRequestDispatcher("admin/custtypelang.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("ShowLoaiKH")) {
                String item = request.getParameter("item");
                LoaiKH_Model lkhm = new LoaiKH_Model();
                Vector v = new Vector();
                v = lkhm.LayDSLKH(langid);
                request.setAttribute("DSLKH", v);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/listcustype.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("ShowLangCT")) {
                String MaLKH = request.getParameter("MaLKH");
                LoaiKH_Model lkhm = new LoaiKH_Model();
                Vector v = new Vector();
                v = lkhm.LayCustomerType(Integer.parseInt(MaLKH));
                request.setAttribute("LangCT", v);
                RequestDispatcher rd = request.getRequestDispatcher("admin/custtypelang.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("CheckTenLKH")) {
                String tenlkh = request.getParameter("tenlkh");
                if (LoaiKH_Model.CheckTenLKH(tenlkh, langid)) {
                    out.print("exist");
                } else {
                    out.print("error");
                }
            }
            else if (action.equals("CheckTenLKHEdit")) {
                String tenlkh = request.getParameter("tenlkh");
                String malkh = request.getParameter("malkh");
                if (LoaiKH_Model.CheckTenLKHEdit(malkh, tenlkh, langid)) {
                    out.print("exist");
                } else {
                    out.print("error");
                }
            }
            else if (action.equals("Edit")) {
                String tenlkh = request.getParameter("tenlkh");
                String malkh = request.getParameter("malkh");
                String chietkhau = request.getParameter("chietkhau");
                String sodiem = request.getParameter("sodiem");
                String mann = request.getParameter("mann");
                LoaiKH_Model.Edit(tenlkh, chietkhau, sodiem, malkh, mann);
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(3, nv.getMaNV(), "customertype");
                LoaiKH_Model lkhm = new LoaiKH_Model();
                Vector v = new Vector();
                v = lkhm.LayCustomerType(Integer.parseInt(malkh));
                request.setAttribute("LangCT", v);
                RequestDispatcher rd = request.getRequestDispatcher("admin/custtypelang.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("Search")) {
                String tenlkh = request.getParameter("tenlkh");
                String sodiem = request.getParameter("sodiem");
                String chietkhau = request.getParameter("chietkhau");
                String item = request.getParameter("item");
                LoaiKH_Model lkhm = new LoaiKH_Model();
                Vector v = new Vector();
                v = lkhm.Search(tenlkh, sodiem, chietkhau, langid);
                request.setAttribute("tenlkh", tenlkh);
                request.setAttribute("sodiem", sodiem);
                request.setAttribute("chietkhau", chietkhau);
                request.setAttribute("item", item);
                request.setAttribute("DSLKH", v);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssearchLKH.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortTenLKH")) {   
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                LoaiKH_Model nmam = new LoaiKH_Model();
                Vector v = new Vector();
                v = nmam.SortTenLKH(langid, order);
                request.setAttribute("DSLKH", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "1");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortLKH.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortSoDiem")) {   
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                LoaiKH_Model nmam = new LoaiKH_Model();
                Vector v = new Vector();
                v = nmam.SortSoDiem(langid, order);
                request.setAttribute("DSLKH", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "2");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortLKH.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortChietKhau")) {   
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                LoaiKH_Model nmam = new LoaiKH_Model();
                Vector v = new Vector();
                v = nmam.SortChietKhau(langid, order);
                request.setAttribute("DSLKH", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "3");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortLKH.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortSearchTenLKH")) {
                String tenlkh = request.getParameter("tenlkh");
                String sodiem = request.getParameter("sodiem");
                String chietkhau = request.getParameter("chietkhau");
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                LoaiKH_Model lkhm = new LoaiKH_Model();
                Vector v = new Vector();
                v = lkhm.SortSearchTenLKH(tenlkh, sodiem, chietkhau, langid, order);
                request.setAttribute("tenlkh", tenlkh);
                request.setAttribute("sodiem", sodiem);
                request.setAttribute("chietkhau", chietkhau);
                request.setAttribute("DSLKH", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "1");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchLKH.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortSearchSoDiem")) {
                String tenlkh = request.getParameter("tenlkh");
                String sodiem = request.getParameter("sodiem");
                String chietkhau = request.getParameter("chietkhau");
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                LoaiKH_Model lkhm = new LoaiKH_Model();
                Vector v = new Vector();
                v = lkhm.SortSearchSoDiem(tenlkh, sodiem, chietkhau, langid, order);
                request.setAttribute("tenlkh", tenlkh);
                request.setAttribute("sodiem", sodiem);
                request.setAttribute("chietkhau", chietkhau);
                request.setAttribute("DSLKH", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "2");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchLKH.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortSearchChietKhau")) { 
                String tenlkh = request.getParameter("tenlkh");
                String sodiem = request.getParameter("sodiem");
                String chietkhau = request.getParameter("chietkhau");
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                LoaiKH_Model lkhm = new LoaiKH_Model();
                Vector v = new Vector();
                v = lkhm.SortSearchChietKhau(tenlkh, sodiem, chietkhau, langid, order);
                request.setAttribute("tenlkh", tenlkh);
                request.setAttribute("sodiem", sodiem);
                request.setAttribute("chietkhau", chietkhau);
                request.setAttribute("DSLKH", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "3");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchLKH.jsp");
                rd.forward(request, response);
            }

        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
